﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Ultima.Xna.Graphics.UI;
using Ultima.Xna.Net;
using Ultima.Xna.Net.Packets;
//using Ultima.Xna.Graphics.UI.Gumps;

namespace Ultima.Xna
{
    public class World
    {
        //Required for LUA
        internal World() { }

        static Game _game;
        
        public static void Initialize(Game game)
        {
            _game = game;
        }

        private static Player _player;
        private static IUIService _uiService;

        private static Dictionary<Serial, Mobile> _mobiles = new Dictionary<Serial,Mobile>();
        private static Dictionary<Serial, Item> _items = new Dictionary<Serial, Item>();
        //private static Dictionary<Serial, Gump> _gumps = new Dictionary<Serial, Gump>();
        public static IUIService UIService
        {
            get { return World._uiService; }
            internal set { World._uiService = value; }
        }

        internal static List<Type> _itemTypes = new List<Type>();
        internal static List<Type> _mobileTypes = new List<Type>();
        
        public static Player Player
        {
            get { return World._player; }
            internal set { World._player = value; }
        }

        //public static Dictionary<Serial, Gump> Gumps
        //{
        //    get { return World._gumps; }
        //}

        public static Dictionary<Serial, Mobile> Mobiles
        {
            get { return _mobiles; }
        }

        public static Dictionary<Serial, Item> Items
        {
            get { return _items; }
        }

        public static IEntity FindEntity(Serial serial)
        {
            if (serial.IsItem)
                return FindItem(serial);
            else if (serial.IsMobile)
                return FindMobile(serial);

            return null;
        }

        public static Mobile FindMobile(Serial serial)
        {
            Mobile mob;

            _mobiles.TryGetValue(serial, out mob);

            return mob;
        }

        public static void AddMobile(Mobile m)
        {
            _mobiles[m.Serial] = m;
        }

        //public static Gump FindGump(Serial serial)
        //{
        //    Gump gump;

        //    _gumps.TryGetValue(serial, out gump);

        //    return gump;
        //}

        public static Item FindItem(Serial serial)
        {
            Item item;

            _items.TryGetValue(serial, out item);

            return item;
        }

        public static void AddItem(Item item)
        {
            _items[item.Serial] = item;
        }

        public static void RemoveMobile(Mobile m)
        {
            _mobiles.Remove(m.Serial);
        }

        public static void RemoveItem(Item item)
        {
            _items.Remove(item.Serial);
        }

        private static void OnWorldItem(IRecvPacket p)
        {
            WorldItemPacket packet = (WorldItemPacket)p;

            if (packet.Serial.IsItem)
            {
                AddItem(new Item(packet.Serial));
            }
            else
            {
                AddMobile(new Mobile(packet.Serial));
            }
        }
    }
}
